package com.xbh.management.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbh.common.utils.JwtUtil;
import com.xbh.management.mapper.DepartmentMapper;
import com.xbh.management.mapper.ManDeptUserMapper;
import com.xbh.management.mapper.SalaryGradeMapper;
import com.xbh.management.mapper.SalaryMapper;
import com.xbh.management.model.ManDepartment;
import com.xbh.management.model.ManDeptUser;
import com.xbh.management.model.ManSalary;
import com.xbh.management.utils.DepartmentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2022-03-15 12:53
 */

@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, ManSalary> implements SalaryService {

    @Autowired
    private ManDeptUserMapper manDeptUserMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private DepartmentUtil departmentUtil;

    @Autowired
    private SalaryGradeMapper salaryGradeMapper;

    @Override
    public Page<ManSalary> list(Page<ManSalary> page, ManSalary salary) {
        LambdaQueryWrapper<ManSalary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManSalary::getDeleted, 0);
        //判断当前登录人是财务部管理者，管理者可查看下级财务部所有员工创建管理的工资
//        LambdaQueryWrapper<ManDeptUser> manDeptUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        manDeptUserLambdaQueryWrapper.eq(ManDeptUser::getUserId,JwtUtil.getUserId());
//        List<ManDeptUser> deptUserList = manDeptUserMapper.selectList(manDeptUserLambdaQueryWrapper);
//        List<Long> deptIdList = deptUserList.stream().map(ManDeptUser::getDepartmentId).collect(Collectors.toList());
//        deptIdList.stream().forEach(deptId ->{
//            LambdaQueryWrapper<ManDepartment> manDepartmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            manDepartmentLambdaQueryWrapper.eq(ManDepartment::getDepartmentId,deptId);
//            ManDepartment department = departmentMapper.selectOne(manDepartmentLambdaQueryWrapper);
//            //id一致则证明当前登录人为财务部管理者
//            if(department.getUserId().equals(JwtUtil.getUserId())){
//
//            }
//        });
        List<Long> userIdList = departmentUtil.returnUserIdListByDepartmentLeader();
        if(CollectionUtil.isNotEmpty(userIdList)){
            wrapper.in(ManSalary::getCreateSalaryId,userIdList);
        }else {
            wrapper.eq(ManSalary::getCreateSalaryId, JwtUtil.getUserId());
        }
        wrapper.orderByDesc(ManSalary::getCreateTime);

        if(StrUtil.isNotEmpty(salary.getSalaryName())){
            wrapper.like(ManSalary::getSalaryName,salary.getSalaryName());
        }
        Page<ManSalary> manSalaryPage = this.baseMapper.selectPage(page, wrapper);
        List<ManSalary> records = manSalaryPage.getRecords();
        if(CollectionUtil.isNotEmpty(records)){
            records.stream().forEach(manSalary ->{
                manSalary.setSalaryGrade(salaryGradeMapper.selectById(manSalary.getSalaryGradeId()));
            });
        }

        return manSalaryPage;
    }

    @Override
    public boolean saveAccount(ManSalary salary) {

        salary.setCreateSalaryId(JwtUtil.getUserId());
        salary.setCreatePerson(JwtUtil.getUsername());
        salary.setUpdatePerson(JwtUtil.getUsername());
        int insert = this.baseMapper.insert(salary);
        if(insert > 0){
            return true;
        }
        return false;
    }
}
